/*
 * Copyright (c) 2018. Beyondsoft Corporation.
 * All Rights Reserved.
 *
 * BEYONDSOFT CONFIDENTIALITY
 *
 * The information in this file is confidential and privileged from Beyondsoft Corporation,
 * which is intended only for use solely by Beyondsoft Corporation.
 * Disclosure, copying, distribution, or use of the contents of this file by persons other than Beyondsoft Corporation
 * is strictly prohibited and may violate applicable laws.
 */

package com.beyondsoft.smarthome.base.model;


import com.beyondsoft.smarthome.base.presenter.BaseFragmentPresenter;
import com.beyondsoft.smarthome.base.presenter.BasePresenter;
import io.reactivex.disposables.Disposable;

/**
 * @author xzk
 * @data 2019/2/26
 * @email xiezhengkun@beyondsoft.com
 * @remark 基类model，负责数据接收
 */
public class BaseFragmentModel<T extends BaseFragmentPresenter> {
    protected T presenter;
    //rx的网络请求对象
    public Disposable disposable;

    public BaseFragmentModel() {
    }


    public void setPresenter(T presenter) {
        this.presenter = presenter;
    }

    /**
     * 回收保持的资源
     */
    public void recycleResource(){
        //回收网络请求（调用本方法表示，当前界面被干掉，那么网络请求也就不能再继续，避免内存泄漏发生）
        if (disposable != null && disposable.isDisposed()) {
            disposable.dispose();
        }
        if(presenter != null){
            presenter = null;
        }
    }

    /**
     * 数据获取监听
     * 成功
     * @param <T>
     */
    public interface OnObjectDataListener<T> {
        void onData(T data);
    }
    //失败
    public interface OnFailLinstener<T> {
        void onFail(T data);
    }
//    错误异常
    public interface OnErrorLinstener<T> {
        void onError(T data);
    }

//    网络请求示例
//    public void addVisitorItinerary(RequestVisitorItineraryDetailsBean visitorItineraryDetailsBean) {
//        LogManagementUtils.showLog(visitorItineraryDetailsBean.getStartTime() + "");
//
//        RequestBody requestBody = RequestBodyGenerateUtils.getRequestBody(visitorItineraryDetailsBean);
//        disposable = Network.getVisitorManagementApi()
//                            .addVisitorItinerary(requestBody)
//                            .subscribeOn(Schedulers.io())
//                            .observeOn(AndroidSchedulers.mainThread())
//                            .subscribe(
//                                new Consumer<VisitorItineraryDetailsBean>() {
//                                    @Override
//                                    public void accept(VisitorItineraryDetailsBean visitorItineraryDetailsBean) throws Exception {
//                                        if (visitorItineraryDetailsBean.getStatus() == ArgumentKey.REQUSET_SUCESS_CODE) {
//                                            LogManagementUtils.showLog(new Gson().toJson(visitorItineraryDetailsBean));
//                                            ToastManagementUtils.showToast(
//                                                context,
//                                                Language.getCreatedSuccessfully(),
//                                                ToastManagementUtils.LENGTHSHORT
//                                                                          );
//                                            context.finish();
//                                        } else {
//                                            ToastManagementUtils.showToast(
//                                                context,
//                                                visitorItineraryDetailsBean.getMessage(),
//                                                ToastManagementUtils.LENGTHSHORT
//                                                                          );
//                                        }
//                                    }
//                                },
//                                throwable -> RequestErrorPopupwindow.showPopupwindow(
//                                    context,
//                                    Language.getRequestError(),
//                                    R.mipmap.icon_request_error
//                                                                                    ));
//    }
}
